//
//  ShowZiXunViewController.m
//  MagazineShangejie
//
//  Created by wen on 15/9/20.
//  Copyright © 2015年 CnbizMedia. All rights reserved.
//

#import "ShowZiXunViewController.h"
#import "Util.h"
#import "UMSocial.h"
@interface ShowZiXunViewController (){
    
    __weak IBOutlet UITableView *wordTable;
    __weak IBOutlet UITableView *questionTable;
    
      NSURL *shareURL;
    NSDictionary *wordDic;
    NSMutableArray *wordArray;
    NSMutableArray *questionDicArray;
    
    UITableViewCell *cell ;
    UITableViewCell *questionCell ;
    NSMutableArray *titleArray;
 
    int cellHight;
}
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *projectName;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollAnchorView;

@end

@implementation ShowZiXunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightBarButton];
    

    
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger number = [userDefault integerForKey:@"buttonTagNumber"];
    
    switch (number) {
        case 0:
            self.title = @"论坛";
            break;
            
        case 1:
            self.title = @"游学";
            break;
            
        case 2:
            self.title = @"咨询";
            break;
            
        case 3:
            self.title = @"创投";
            break;
            
        default:
            break;
    }

    titleArray = [[NSMutableArray alloc]initWithObjects:@"项目介绍", nil];
    
    NSDictionary *lunTanDic = [userDefault objectForKey:@"lunTanDic"];
    
    NSString *idString = [lunTanDic objectForKey:@"id"];
    NSString *catid    = [lunTanDic objectForKey:@"catid"];
    
    NSDictionary*parameter;
    parameter=@{@"catid":catid,@"id":idString};
    
    [[AppAPIClient sharedClient] GET:@"Club/Show" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"operation = =%@",operation);
        shareURL = operation.request.URL;
        NSLog(@"shareURL -- %@",shareURL);
        
        NSMutableDictionary *result = responseObject[@"data"];
        NSLog(@"luntan-data == %@",result);
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if([result isKindOfClass:[NSNull class]] || [result count] == 0||[result[@"content"] count]==0){
            return;
        }
        
        wordDic = nil;
        
        wordDic = [[NSDictionary alloc] initWithDictionary:result[@"content"] copyItems:YES ];
        
            _projectName.text = [wordDic objectForKey:@"title"];
        
        
        //设置头图的照片
        [_headImage sd_setImageWithURL:[NSURL URLWithString:wordDic[@"thumb"]]];
        
        
        
        /*
      
         */
        wordArray = [[NSMutableArray alloc] initWithObjects:[wordDic objectForKey:@"content"],nil];
        NSLog(@"wordArray -- %@",wordArray);
        
//        questionDicArray = [[NSMutableArray alloc] initWithArray:result[@"answer"] ];
//        NSLog(@"answer-questionDicArray == %@",questionDicArray);
        
        [wordTable reloadData];
        [questionTable reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

    
    
    
    //找到anchor view的高度约束修改高度
    NSArray *arrs = _scrollAnchorView.constraints;
    NSLog(@"arrs == %@",_scrollAnchorView.constraints);
    
    for (NSLayoutConstraint *attr in arrs) {
        if(attr.firstAttribute == NSLayoutAttributeHeight){
            
            NSLog(@"self.view.frame.size.height == %f",self.view.frame.size.height);
            attr.constant = self.view.frame.size.height +150;
            attr.constant = 1750;
            NSLog(@"attr.constant == %f",attr.constant);
        }
    }
    //更新父视图约束条件
    [_scrollAnchorView.superview.superview updateConstraints];
    
    //
    wordTable.separatorStyle = NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setRightBarButton
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"share01"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button addTarget:self action:@selector(rightBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;
}

//分享按钮
- (void)rightBarButtonClicked
{
    NSString *URLString = [NSString stringWithFormat:@"%@",shareURL];
    
    BDSocialShareContent *content = [BDSocialShareContent shareContentWithDescription:@"超百万企业家都在看的商业财经类阅读客户端" url:URLString title:@"看商界"];
    //    [content addImageWithThumbImage:[UIImage imageNamed:@"icon_default_ipad"] imageUrl:self._Dict[@"thumb"]];
    UIImageView*imageview=[[UIImageView alloc]init];
    imageview.image=[UIImage imageNamed:@"icon"];
    [SVProgressHUD dismiss];
    //分享
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:nil
                                      shareText:[NSString stringWithFormat:@"%@%@",[wordDic objectForKey:@"title"],URLString]
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,UMShareToWechatSession,UMShareToTencent,UMShareToEmail,UMShareToSms,nil]
                                       delegate:self];
    
    [UMSocialData defaultData].extConfig.qqData.url = URLString;
    [UMSocialData defaultData].extConfig.qzoneData.url = URLString;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = URLString;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = URLString;
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = URLString;
    
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];
    
    
    
//    NSLog(@"wordTable.frame == %f",wordTable.frame.size.height);
}

-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData
{
    if (platformName == UMShareToSina) {
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端%@",[NSString stringWithFormat:@"%@",shareURL]];
    }
    else{
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端"  ];
    }
}

#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    if (section==0) {
    //        return [self tableHeader];
    //    }else{
    return nil;
    //    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([tableView isEqual:wordTable]){
        return wordArray.count;
    }
    
    else if ([tableView isEqual:questionTable])
        
    {
        return questionDicArray.count;
    }
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
    ////    = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    //
    
    
    if ([tableView isEqual:wordTable]){
        NSLog(@"cell height %f",cell.frame.size.height);
        
        return cell.frame.size.height;
    }
    
    else if ([tableView isEqual:questionTable])
        
    {
        return 120;
    }
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([tableView isEqual:wordTable]) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"wordCell"];
            
        }
        
        UILabel*label5=(UILabel*)[cell.contentView viewWithTag:11];
        label5.text = @"项目介绍";
        label5.text = titleArray[indexPath.row];
        
        UILabel*label7=(UILabel*)[cell.contentView viewWithTag:22];
        label7.text = @"详细介绍";
        label7.text = wordArray[indexPath.row];
        label7.numberOfLines = 0;
        [label7 sizeToFit];
        
        //    label7.text = @"agduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwj";
        
        NSLog(@"label5.text -- %@",label5.text);
        NSLog(@"label7.text -- %@",label7.text);
        
        CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
        cell.userInteractionEnabled = NO;
        
        CGSize requiredSize = [label7.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
        
        CGRect rect = cell.frame;
        rect.size.height = requiredSize.height+100;
        //    label7.frame.size.height = requiredSize.height + 30;
        cellHight = rect.size.height;
        NSLog(@"cellHight -- %d",cellHight);
        cell.frame = rect;
        
        UIImageView *linImage = (UIImageView *)[cell.contentView viewWithTag:33];
        linImage.backgroundColor = [UIColor grayColor];
        
        
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        
        return cell;
    }
    
    
    else if ([tableView isEqual:questionTable])
        
    {
        
        static NSString *identifier = @"questionCell";
        
        questionCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (!questionCell) {
            
            questionCell=[tableView dequeueReusableCellWithIdentifier:@"questionCell"];
        }
        
        NSDictionary *questionDic = questionDicArray[indexPath.row];
        
        NSLog(@"questionDicArray == %@",questionDicArray);
        NSLog(@"questionDic == %@",questionDic);
        UILabel*label1=(UILabel*)[questionCell.contentView viewWithTag:111];
        label1.text = @"188***21";
        label1.text = [questionDic objectForKey:@"name"];
        
        //           reply introduce
        UILabel*label2=(UILabel*)[questionCell.contentView viewWithTag:222];
        label2.text = @"问题";
        label2.text = [questionDic objectForKey:@"introduce"];
        NSLog(@"[questionDic objectForKey:@introduce]%@",[questionDic objectForKey:@"introduce"]);
        label2.numberOfLines = 0;
        [label2 sizeToFit];
        
        UIImageView *view = (UIImageView *)[questionCell.contentView viewWithTag:444];
        view.backgroundColor = [UIColor grayColor];
        
        UILabel*label4=(UILabel*)[questionCell.contentView viewWithTag:555];
        label4.text = @"回答";
        label4.text = [questionDic objectForKey:@"reply"];
        label4.numberOfLines = 0;
        [label4 sizeToFit];
        NSLog(@"[questionDic objectForKey:@reply]%@",[questionDic objectForKey:@"reply"]);
        
        UILabel*label3=(UILabel*)[questionCell.contentView viewWithTag:333];
        label3.text = @"2015-1-12";
        
        UILabel*label5=(UILabel*)[questionCell.contentView viewWithTag:666];
        label5.text = @"2015-1-12";
        
        NSString *timeString = [questionDic objectForKey:@"addtime"];
        
        NSLog(@"timeString -- %@",timeString);
        //得到运动时间，需要转成年月日
        
        NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSString *date =  [formatter stringFromDate:[NSDate date]];
        NSString *timeLocal = [[NSString alloc] initWithFormat:@"%@", date];
        NSLog(@"timeLocal%@", timeLocal);
        label3.text = timeLocal;
        label4.text = timeLocal;
        
        
        questionCell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        return questionCell;
    }
    
    
    
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    indexPath.row;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
    //    NSDictionary *lunTanDic = userData[indexPath.row];
    //    NSLog(@"lunTanDic == %@",lunTanDic);
    //
    //    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //    [userDefault setObject:lunTanDic forKey:@"lunTanDic"];
    //    [userDefault synchronize];//存入文件
    //
    //
    //    if ([self.title isEqualToString:@"论坛"]) {
    //        [self performSegueWithIdentifier:@"IWanIdentifier" sender:indexPath];
    //    }
    
    
}

@end
